This is a race.
Field appenderList in class
org.apache.log4j.helpers.AppenderAttachableImpl is accessed by
multiple threads without protection. An ArrayIndexOutOfBoundsException
is raised due to the bug.
More details about this bug are at LOG4J-54325
JIRA page.
This happens at org.apache.log4j.helpers.AppenderAttachableImpl:
The
numbers present the global execution sequence.
org.apache.log4j.helpers.AppenderAttachableImpl
t1 t2 public void removeAllAppenders() {
if(appenderList != null) {
int len = appenderList.size();
for(int i = 0; i < len; i++) {
2 Appender a = (Appender) appenderList.elementAt(i);
a.close();
}
1 appenderList.removeAllElements();
appenderList = null;
}
}
a) t1 removes all appenderList's elements at 1.
b) Context switched, t2 starts to get appenderList's element at 2. However, appenderList is empty now, so an ArrayIndexOutOfBoundsException is thrown out.
This bug is reproduced under log4j 1.2.13 and JDK 1.6.0_33.
Execute
the following scripts to run the test to reproduce the bug (assume the
location of the log4j test project is log4j_test_home).
Linux:
${log4j_test_home}/scripts/54325.sh
Windows:
%log4j_test_home%\scripts\54325.bat